package com.cwj;

import com.cwj.mapper.PersonMapper;
import com.cwj.model.Person;
import com.cwj.util.RedisUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
public class HelloController {

    @Resource
    PersonMapper personMapper;
    @Resource
    private RedisUtil<Person> redisUtil;
    @GetMapping("test")
//    @Cacheable(value="person",key = "1")
    public Person test(){
        System.out.println("查询数据返回-->");
        Person person = new Person();
        person.setAddress("上海");
        person.setId(1);
        person.setName("tom");
        person.setAge(18);
        person.setDate(new Date());
        person.setTime(LocalDateTime.now());
        return person;
    }

    @GetMapping("test2")
//    @CachePut(value="person", key = "1")
    public Person test2(){
        Person person1 = redisUtil.get("person::1");
        System.out.println(person1);
        Person person = new Person();
        person.setAddress("北京");
        person.setId(1);
        person.setName("tom");
        person.setDate(new Date());
        person.setTime(LocalDateTime.now());
        if(person1 == null){
            person.setAge(18);
        }else{
            person.setAge(person1.getAge() +1);
        }
        return person;
    }

    @PostMapping("test3")
    public Person test3(@RequestBody Person person){
        return person;
    }
    @GetMapping("test4")
    public List<Person> test4(@RequestParam("date")LocalDate date, @RequestParam("name")String name){
//        List<Person> list = personMapper.getPersonList(date, name);
        System.out.println(date);
        System.out.println(name);
        Person byId = personMapper.getById(1);
        System.out.println(byId);
        List<Person> personList = personMapper.getPersonList(date, name);
        return personList;
    }

    @PostMapping("add")
    public Person add(@RequestBody Person person){
        personMapper.add(person);
        return person;
    }
}
